package com.ccys.cms.service.consultation.impl;


import com.alibaba.fastjson.JSONObject;
import com.ccys.cms.service.consultation.BookingAppointmentWebService;
import com.ccys.common.util.StringUtil;
import com.ccys.common.vo.ResponseResult;
import com.ccys.db.cache.UserUtil;
import com.ccys.db.dto.consultation.BookingDTO;
import com.ccys.db.entity.TblBooking;
import com.ccys.db.entity.TblBookingAppointmentConfirmLog;
import com.ccys.db.entity.TblPlanBenefit;
import com.ccys.db.entity.TblPlanBenefitMember;
import com.ccys.db.mapper.TblBookingAppointmentConfirmLogMapper;
import com.ccys.db.mapper.TblBookingMapper;
import com.ccys.db.mapper.TblDoctorSpecialisationMapper;
import com.ccys.db.mapper.TblPlanBenefitMemberMapper;
import com.ccys.db.page.PageUtils;
import com.ccys.db.rely.service.impl.BaseServiceImpl;
import com.ccys.db.service.BaseTblAuditTrailWebService;
import com.ccys.db.vo.consultation.BookingAppointmentDetailWebVO;
import com.ccys.db.vo.consultation.BookingAppointmentMemberBenefitList;
import com.ccys.db.vo.consultation.BookingAppointmentWebVO;

import com.ccys.db.vo.consultation.DockerSpecialisationListWebVO;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;


/**
 * tbl_booking管理
 */
@Service
@Slf4j
public class BookingAppointmentWebServiceImpl extends BaseServiceImpl<TblBooking> implements BookingAppointmentWebService {

    @Autowired(required = false)
    private TblBookingMapper bookingMapper;
    @Autowired(required = false)
    private BaseTblAuditTrailWebService baseTblAuditTrailWebService;

    @Autowired(required = false)
    private TblDoctorSpecialisationMapper tblDoctorSpecialisationMapper;

    @Autowired(required = false)
    private TblPlanBenefitMemberMapper tblPlanBenefitMemberMapper;
    @Autowired(required = false)
    private TblBookingAppointmentConfirmLogMapper bookingAppointmentConfirmLogMapper;

    @Override
    public ResponseResult listObjectsCms(HttpServletRequest request, HttpServletResponse response, BookingDTO dto) {
        dto.setIsAppointment(1);
        PageUtils.startPage(request);
        List<BookingAppointmentWebVO> list = this.bookingMapper.listObjectsCms(dto);
        PageInfo pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }


    @Override
    public ResponseResult change(TblBooking entity) {
        if(entity.getAppointmentType() != null) {
            if(entity.getAppointmentType() == 1) {
                entity.setConsultationMode(5);
            }else  if(entity.getAppointmentType() == 2) {
                entity.setConsultationMode(1);
            }else  if(entity.getAppointmentType() == 3) {
                entity.setConsultationMode(6);
            }
        }
        if (StringUtils.isBlank(entity.getId())) {
            //此处添加 为预约
            entity.setIsAppointment(1);
            //预约状态 前端传
            entity.setAppointmentCreatedOn(new Date());
            // 新增
            this.saveEntity(entity);
        } else {
            // 编辑
            this.updateEntityById(entity);
            if(entity.getAppointmentStatus() != null) {
                this.baseTblAuditTrailWebService.saveStatusLog(entity,entity.getAppointmentStatus());
            }
            //只要修改就产生记录
            saveBookingAppointmentConfirmLog(entity.getId());

        }
        return ResponseResult.success();
    }

    private void saveBookingAppointmentConfirmLog(String bookingId) {
        TblBooking entityById = this.findEntityById(bookingId);
        TblBookingAppointmentConfirmLog bookingAppointmentConfirmLog = new TblBookingAppointmentConfirmLog();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("mem_id",entityById.getMemId());
        jsonObject.put("child_id",entityById.getPatientMemId());
        jsonObject.put("doctor_id",entityById.getDoctorId());
        jsonObject.put("booking_type",entityById.getMemType());
        jsonObject.put("consult_type",entityById.getConsultType());
        jsonObject.put("booking_consultation_type",entityById.getConsultationMode());
        jsonObject.put("appointment_time",entityById.getAppointmentTime());
        jsonObject.put("appointment_note",entityById.getAppointmentNote());
        jsonObject.put("appointment_type",entityById.getAppointmentType());
        jsonObject.put("appointment_status",entityById.getAppointmentStatus());
        jsonObject.put("specialisation_id",entityById.getSpecialisationId());
        jsonObject.put("appointment_end_time",entityById.getAppointmentEndTime());
        jsonObject.put("AC_reason_id",entityById.getAcReasonId());
        jsonObject.put("benefit_id",entityById.getBenefitId());
        bookingAppointmentConfirmLog.setBookingId(bookingId);
        bookingAppointmentConfirmLog.setJson(jsonObject.toJSONString());
        bookingAppointmentConfirmLog.setUserId(UserUtil.getCurLoginUserId());
        bookingAppointmentConfirmLog.setAppointmentStatus(entityById.getAppointmentStatus());
        this.bookingAppointmentConfirmLogMapper.insert(bookingAppointmentConfirmLog);
    }

    @Override
    public ResponseResult delete(TblBooking dto) {
        TblBooking entity = new TblBooking();
        entity.setId(dto.getId());
        entity.setDelFlag(1);
        this.updateEntityById(entity);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getBookingAppointmentDetail(BookingDTO dto) {
        BookingAppointmentDetailWebVO bookingAppointmentDetailWebVO = this.bookingMapper.getBookingAppointmentDetail(dto);
        return ResponseResult.success(bookingAppointmentDetailWebVO);
    }

    @Override
    public ResponseResult dockerSpecialisationList(BookingDTO dto) {
        List <DockerSpecialisationListWebVO> list = this.tblDoctorSpecialisationMapper.getDockerSpecialisationList(dto.getDoctorId());
        return ResponseResult.success(list);
    }

    @Override
    public ResponseResult listBookingAppointmentMemberBenefit(BookingDTO dto) {
        if(StringUtil.isNotEmpty(dto.getSpecialisationId())) {
            //前端传的科室  identification 值要做一个转换。
            // tbl_specialisation里的indentification等于tbl_booking里的specialisation_id
            //identification中的2和3对应的是tbl_plan_benefit_service里service_type的2(mental wellness);1对应的是3(peadiatrics)
            if("2".equals(dto.getSpecialisationId()) || "3".equals(dto.getSpecialisationId())) {
                //对应服务类型的2  去查询
                dto.setSpecialisationId("2");
            }
            if("1".equals(dto.getSpecialisationId())) {
                //对应服务类型的3  去查询
                dto.setSpecialisationId("3");
            }

        }
        List <BookingAppointmentMemberBenefitList> list = this.tblPlanBenefitMemberMapper.listBookingAppointmentMemberBenefit(dto.getMemId(),dto.getSpecialisationId());
        return ResponseResult.success(list);
    }


}